 //
//  Preson.m
//  TLock
//
//  Created by lenny on 2017/11/3.
//  Copyright © 2017年 aliyun. All rights reserved.
//
#import <React/RCTLog.h>

#import "T11Plugin.h"


@implementation T11Plugin

RCT_EXPORT_MODULE();

    
RCT_EXPORT_METHOD(scan){
    dispatch_sync(dispatch_get_main_queue(), ^{
        //发送打开扫描仪的通知
        [[NSNotificationCenter defaultCenter]postNotificationName:@"OpenScan" object:nil];
    });
}
RCT_EXPORT_METHOD(bindMsgChannel){
    dispatch_sync(dispatch_get_main_queue(), ^{
        //绑定消息通道
        [[NSNotificationCenter defaultCenter]postNotificationName:@"bindMsgChannel" object:nil];
    });
}
RCT_EXPORT_METHOD(unbindMsgChannel){
    dispatch_sync(dispatch_get_main_queue(), ^{
        //解绑消息通道
        [[NSNotificationCenter defaultCenter]postNotificationName:@"unbindMsgChannel" object:nil];
    });
}
//RCT_EXPORT_METHOD(logoutYK){
//    dispatch_sync(dispatch_get_main_queue(), ^{
//        //登出怡康服务器
//        [YKBusinessFramework equesLogout];
//    });
//}
//RCT_EXPORT_METHOD(loginYK){
//    dispatch_sync(dispatch_get_main_queue(), ^{
//        //登录怡康服务器
//        [[NSNotificationCenter defaultCenter]postNotificationName:@"loginYK" object:nil];
//    });
//}
//RCT_EXPORT_METHOD(toEyes){
//    dispatch_sync(dispatch_get_main_queue(), ^{
//        //跳转猫眼
//        [[NSNotificationCenter defaultCenter]postNotificationName:@"toEyes" object:nil];
//    });
//}
RCT_EXPORT_METHOD(getVersion:(RCTResponseSenderBlock)callback){
    dispatch_sync(dispatch_get_main_queue(), ^{
        //获取APP版本
        NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
        NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
        NSLog(@"当前的版本是 %@",currentVersion);
        callback(@[[NSNull null],currentVersion]);
    });
}
RCT_EXPORT_METHOD(openPhoto:(NSString *)url){
    dispatch_sync(dispatch_get_main_queue(), ^{
        //打开相册
//         url = @"http://www.team11.cn:9091/t11_gaokemei/uploadFile?fname=jtr123.png";
        //https://github.com/banchichen/TZImagePickerController
        [[NSNotificationCenter defaultCenter]postNotificationName:@"openPhoto" object:nil userInfo:@{@"url":url}];
    });
}
RCT_EXPORT_METHOD(openCamera:(NSString *)url){
    dispatch_sync(dispatch_get_main_queue(), ^{
        //打开相机
        //https://github.com/tdginternet/TGCameraViewController
        [[NSNotificationCenter defaultCenter]postNotificationName:@"openCamera" object:nil userInfo:@{@"url":url}];
    });
}
RCT_EXPORT_METHOD(exit){
    dispatch_sync(dispatch_get_main_queue(), ^{
        //退出app
        exit(0);
    });
}
    
-(NSArray *)supportedEvents {
    return@[
            @"EventReminder_BarcodeScanSuccessForRN",
            @"EventReminder_CollectionProductSuccessForRN",
            @"BarcodeScanSuccessForRN",
            @"EventReminder_ResultForUpload"
          ];
}
- (void)startObserving {
//    [[NSNotificationCenter defaultCenter] addObserver:self
//                                             selector:@selector(CollectionProductSuccessForRNAction:)
//                                                 name:@"CollectionProductSuccessForRN"
//                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(BarcodeScanSuccessForRNAction:)
                                                 name:@"BarcodeScanSuccessForRN"
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ResultForUpload:) name:@"ResultForUpload" object:nil];
}
- (void)BarcodeScanSuccessForRNAction:(id)obj {
    //    [self.bridge.eventDispatcher sendAppEventWithName:@"EventReminder_BarcodeScanSuccessForRN"
    
    dispatch_async(dispatch_get_main_queue(), ^{
        NSString *qrCode = [obj valueForKey:@"object"];
        [self sendEventWithName:@"EventReminder_BarcodeScanSuccessForRN" body:@{@"qrCode": qrCode}];
        
    });
    
}
-(void)ResultForUpload:(NSNotification *)notification{
    dispatch_async(dispatch_get_main_queue(),^{
        id error = [notification.userInfo valueForKey:@"error"];
        id sucess = [notification.userInfo valueForKey:@"success"];
        error = [NSString stringWithFormat:@"%@",error];
        sucess = [NSString stringWithFormat:@"%@",sucess];
        [self sendEventWithName:@"EventReminder_ResultForUpload" body:@{@"error":error,@"success":sucess}];
//        [self sendEventWithName:@"ResultForUpload" body:@{@"error":error}];
    });
}
//- (void)CollectionProductSuccessForRNAction:(NSNotification*)notification {
//    
//    dispatch_async(dispatch_get_main_queue(), ^{
//        [self sendEventWithName:@"EventReminder_CollectionProductSuccessForRN" body:nil];
//    });
//    
//}

- (void)stopObserving {
  
//    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"CollectionProductSuccessForRN" object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"BarcodeScanSuccessForRN" object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"EventReminder_ResultForUpload" object:nil];
}

//打开相机
//-(void)openCamera{
//    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
//
//    // You can get the photos by block, the same as by delegate.
//    // 你可以通过block或者代理，来得到用户选择的照片.
//    //    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets) {
//    //        NSLog(@"%@",assets);
//    //    }];
//
//    [self presentViewController:imagePickerVc animated:YES completion:nil];
//}

@end
